package edu.whu.webgis.controller;


import edu.whu.webgis.entity.VPosting;
import edu.whu.webgis.entity.VSimplePosting;
import edu.whu.webgis.service.IPostingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/posting")
public class PostingController {
    @Autowired
    IPostingService service;


    @PostMapping("/publish")
    public ResponseEntity<String> publishPosting(@RequestBody VPosting posting){
        service.publish(posting);
        return ResponseEntity.ok("Success");
    }

    @GetMapping("/get")
    public ResponseEntity<List<VSimplePosting>> getPostings(@RequestParam String uid){
        List<VSimplePosting> postings = service.getPostingByUid(uid);
        if (postings.size() == 0)
            return ResponseEntity.noContent().build();
        else
            return ResponseEntity.ok(postings);
    }

    @GetMapping("/collections")
    public ResponseEntity<List<VSimplePosting>> getCollections(@RequestParam String uid){
        List<VSimplePosting> postings = service.getCollections(uid);
        if (postings.size() == 0)
            return ResponseEntity.noContent().build();
        else
            return ResponseEntity.ok(postings);
    }


    @GetMapping("/detail")
    public ResponseEntity<VPosting> getDetail(@RequestParam Integer id){
        VPosting posting = service.getDetail(id);
        return ResponseEntity.ok(posting);
    }

    @GetMapping("/page/count")
    public ResponseEntity<Long> getPage(@RequestParam Integer size,@RequestParam String keyword){
        return ResponseEntity.ok(service.getPageCount(size,keyword));
    }

    @GetMapping("/page/get")
    public ResponseEntity<List<VSimplePosting>> getPage(@RequestParam Integer size, @RequestParam Integer pos,@RequestParam String keyword){
        List<VSimplePosting> postings = service.selectPage(size,pos,keyword);
        if (postings.size() == 0)
            return ResponseEntity.noContent().build();
        else
            return ResponseEntity.ok(postings);
    }


    @GetMapping("/condition")
    public ResponseEntity<List<VSimplePosting>> getPostingByCondition(@RequestParam String keyword){
        List<VSimplePosting> postings = service.selectByCondition(keyword);
        if (postings.size() == 0)
            return ResponseEntity.noContent().build();
        else
            return ResponseEntity.ok(postings);
    }





}
